﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;

namespace TradeMarkSys
{
    public partial class FrmDelTrademark : Form
    {
        public FrmDelTrademark()
        {
            InitializeComponent();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryTrademark();
        }

        private void toolStripMenuItemDel_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = null;
            try
            {
                var row = dataGridViewTrademark.CurrentRow;
                if (row != null)
                {
                    var id = dataGridViewTrademark.CurrentRow.Cells[0].Value;
                    string sql = string.Format(@"delete from TradeMark where id = {0}", id);
                    string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Windows.Forms.Application.StartupPath + "\\TradeMarkSys.mdb";
                    conn = new OleDbConnection(connstr);
                    OleDbCommand cmd = new OleDbCommand(sql, conn);
                    conn.Open();
                    if (cmd.ExecuteNonQuery() > 0)
                    {
                        MessageBox.Show("删除成功");
                    }
                    else
                    {
                        MessageBox.Show("删除失败,请重试！");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
            QueryTrademark();
        }

        private void QueryTrademark()
        {
            OleDbConnection conn = null;
            try
            {
                string where = " where 1=1 ";
                if (!txtRegisterNumber.Text.Equals(""))
                {
                    where += string.Format(" and BaseInfo like '%{0}%'", txtRegisterNumber.Text);
                }
                if (!txtRegCode.Text.Equals(""))
                {
                    where += string.Format(" and RegCode like '%{0}%'", txtRegCode.Text);
                }
                if (!txtApplicant.Text.Equals(""))
                {
                    where += string.Format(" and Applicant like '%{0}%'", txtApplicant.Text);
                }
                if (!txtTradeMarkName.Text.Equals(""))
                {
                    where += string.Format(" and TradeMarkName like '%{0}%'", txtTradeMarkName.Text);
                }
                if (!cmbApplyProject.Text.Equals(""))
                {
                    where += string.Format(" and ApplyProject = '{0}'", cmbApplyProject.Text);
                }
                string sql = string.Format(@"select * from TradeMark {0}", where);
                string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Windows.Forms.Application.StartupPath + "\\TradeMarkSys.mdb";
                conn = new OleDbConnection(connstr);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds);
                dataGridViewTrademark.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
    }
}
